package com.nf.health.app.sport_recipe.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.fragment.IBaseFragment;
import com.nf.health.app.http.Act;
import com.nf.health.app.sport_recipe.activity.SportRecipeActivity;
import com.nf.health.app.sport_recipe.activity.SportRecipeHisActivity;
import com.nf.health.app.sport_recipe.bean.EventBusChangeRecordBean;
import com.nf.health.app.sport_recipe.bean.SportRecipeDetailBean;
import com.nf.health.app.sport_recipe.customview.AerobicExerciseView;
import com.nf.health.app.sport_recipe.customview.MovementExerciseView;
import com.nf.health.app.sport_recipe.customview.StretcheExerciseView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by he_pan on 2017/4/18.
 * The only genius that is worth anything is the genius for hard work
 *
 * @author he_pan
 * @Description 运动处方
 */

public class SportRecipeFragment extends IBaseFragment {

    @Bind(R.id.tv_steps)
    TextView tvSteps;
    @Bind(R.id.tv_p1)
    TextView tvP1;
    @Bind(R.id.tv_p2)
    TextView tvP2;
    @Bind(R.id.ll_parent_content)
    LinearLayout llParentContent;
    private String hpuid;
    private SportRecipeDetailBean mSportRecipeBean;
    private AerobicExerciseView aerobicView;
    private MovementExerciseView movementView;
    private StretcheExerciseView stretcheView;
    private EventBusChangeRecordBean eventbusBean;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        addViewFillInRoot(R.layout.fragment_sport_recipe);
        ButterKnife.bind(this, rootView);
        EventBus.getDefault().register(this);
        initData();
    }


    private void initData() {
        if(getActivity() instanceof SportRecipeActivity){
            provider.getLatestSportRecipeDetail(SportRecipeActivity.hpuid);
        }

        if(getActivity() instanceof SportRecipeHisActivity){
            provider.getLatestSportRecipeDetail(SportRecipeHisActivity.hpuid);
        }
        //provider.getLatestSportRecipeId();
//        provider.getLatestSportRecipeDetail("87");
    }

    @Override
    public void handleActionSuccess(String actionName, Object object) {
        if (Act.Flag.FLAG_GET_LATEST_SPORTRECIPE_ID.equals(actionName)) {
            hpuid = (String) object;
            if ("0".equals(hpuid)) {
//                llParentContent.removeAllViews();
//                View v = new HaveNoSportView(getActivity());
//                llParentContent.addView(v);
                llParentContent.setVisibility(View.INVISIBLE);
                showNoData();//没有数据,主界面已经判断,这里不会触发
            } else {
                provider.getLatestSportRecipeDetail(hpuid);
            }
        } else if (Act.Flag.FLAG_GET_SPORTRECIPE_DETAIL.equals(actionName)) {
            mSportRecipeBean = (SportRecipeDetailBean) object;
            setUpView(mSportRecipeBean);
        } else if (Act.Flag.FLAG_RECORD_SPORT_TAKE.equals(actionName)) {
            //改变flag对应view
            refreshFlagView();
        }
    }

    private void refreshFlagView() {
        switch (eventbusBean.getType()) {
            case "1"://有氧运动
                mSportRecipeBean.getAerobics().setFlag("1");
                aerobicView.refreshFlagView(mSportRecipeBean);
                break;
            case "2":
                mSportRecipeBean.getMovement().setFlag("1");
                movementView.refreshFlagView(mSportRecipeBean);
                break;
            case "3":
                mSportRecipeBean.getStretche().setFlag("1");
                stretcheView.refreshFlagView(mSportRecipeBean);
                break;
            default:
                break;
        }
    }

    private void setUpView(SportRecipeDetailBean bean) {
        llParentContent.setVisibility(View.VISIBLE);
        //运动建议布局直接写xml里
        tvSteps.setText(bean.getStep());
        tvP1.setText(bean.getP1());
        tvP2.setText(bean.getP2());
        //有氧运动,自定义
        aerobicView = new AerobicExerciseView(getActivity(), bean);
        llParentContent.addView(aerobicView);
        //阻抗运动
        movementView = new MovementExerciseView(getActivity(), bean);
        llParentContent.addView(movementView);
        //拉伸运动
        stretcheView = new StretcheExerciseView(getActivity(), bean);
        llParentContent.addView(stretcheView);
    }

    /**
     * 记录完成运动
     *
     * @param bean
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void changeSportsTake(EventBusChangeRecordBean bean) {
        System.out.println("收到通知");
        if ("1".equals(bean.getFlag())) {
            showToast("已记录");
            return;
        } else {
            eventbusBean = bean;
            provider.recordSportTake(mSportRecipeBean.getInterneneid(), bean.getType());
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
        EventBus.getDefault().unregister(getActivity());
    }
}
